package org.leo.dm.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

/**
 * 
 * Created on 2015年11月13日
 * <p>
 * Description 流方式写入XML
 * <p>
 * Copyright neusoft (c) 2015 .
 * 
 * @author xiao_h
 *
 */
public class SaxWriteXmlUtil {
	SAXTransformerFactory fac = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
	private TransformerHandler handler;
	private OutputStream outStream;
	private String fileName;
	private AttributesImpl atts;

	private String rootElement;

	public SaxWriteXmlUtil(String fileName, String rootElement) {
		this.fileName = fileName;
		this.rootElement = rootElement;
		init();
	}

	public void init() {
		try {
			handler = fac.newTransformerHandler();
			Transformer transformer = handler.getTransformer();
			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");//
			// 设置输出采用的编码方式
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");// 是否自动添加额外的空白
			transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");// 是否忽略xml声明
			outStream = new FileOutputStream(fileName);
			Result resultxml = new StreamResult(outStream);
			handler.setResult(resultxml);
			atts = new AttributesImpl();

			start();
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	private void start() {
		try {
			handler.startDocument();
			handler.startElement("", "", rootElement, atts);
		} catch (SAXException e) {
			e.printStackTrace();
		}
	}

	public void write(HashMap<Object, Object> map, String objectElement) throws SAXException {
		Set<Object> keys = map.keySet();
		Iterator<Object> it = keys.iterator();
		if (objectElement != null) {
			handler.startElement("", "", objectElement, atts);
		}
		String key = null;
		String value = null;
		while (it.hasNext()) {
			key = (String) it.next();
			value = (String) map.get(key);

			handler.startElement("", "", key, atts);
			handler.characters(value.toCharArray(), 0, value.length());
			handler.endElement("", "", key);
		}
		if (objectElement != null) {
			handler.endElement("", "", objectElement);
		}
	}

	public void write(String value, String objectElement) throws SAXException {
		if (objectElement != null) {
			handler.startElement("", "", objectElement, atts);
		}

		handler.characters(value.toCharArray(), 0, value.length());

		if (objectElement != null) {
			handler.endElement("", "", objectElement);
		}
	}

	public void startElement(String objectElement) throws SAXException {
		if (objectElement != null) {
			handler.startElement("", "", objectElement, atts);
		}
	}

	public void endElement(String objectElement) throws SAXException {
		if (objectElement != null) {
			handler.endElement("", "", objectElement);
		}
	}

	public void end() {
		try {
			handler.endElement("", "", rootElement);
			handler.endDocument();// 文档结束,同步到磁盘
			outStream.close();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		SaxWriteXmlUtil xml = new SaxWriteXmlUtil("c:/student.xml", "students");
		try {
			HashMap<Object, Object> map = new HashMap<Object, Object>();
			map.put("id", "20050505");
			map.put("name", "zhaobenshan");
			map.put("age", "21");
			map.put("classes", "Act051");
			xml.write(map, "student");

			map = new HashMap<Object, Object>();
			map.put("id", "20050506");
			map.put("name", "songdandan");
			map.put("age", "20");
			map.put("classes", "Act052");

			xml.write(map, "student");

			map = new HashMap<Object, Object>();
			map.put("id", "20050507");
			map.put("name", "fanchushi");
			map.put("age", "21");
			map.put("classes", "Act051");

			xml.write(map, "student");

			xml.end();
		} catch (SAXException e) {
			e.printStackTrace();
		}
	}
}
